<?php
class Controller_ProjectDemo extends Controller_Website {
    
    public function action_list()
    {
        $model = Model::factory('demo1');
        $where = array(
            'is_del'=>1,//排除掉记录为已删除的demo
        );
        $total = $model->count($where);
        $pager = new Pager($total, 10);
        $model = $model->select($pager->offset, $pager->size, $where)->as_array(); 

        $m_pro_cat = Model::factory('projectCategory');
        $m_prof_cat = Model::factory('professionCategory');
        foreach ($model as &$m)
        {
            $pro_cat = $m_pro_cat->getRow(array('id'=>$m['project_cid']));
            $m['project_cid_name'] = $pro_cat['name'];
            $prof_cat = $m_prof_cat->getRow(array('id'=>$m['profession_cid']));
            $m['profession_cid_name'] = $prof_cat['name'];
        }
        $this->content = View::factory('projectDemo/list');
        $this->content->model = $model;
        $this->content->pager = $pager;
    }

    //添加demo
    public function action_add()
    {
        $m_project = Model::factory('demo1');
        $video = Model::factory('video');
        if (!empty($_POST)) {
            $data = $_POST;
            $data['project_begin'] = strtotime($_POST['project_begin']);
            $data['project_end'] = strtotime($_POST['project_end']);
            $data['status'] = 2;
            $pic = $video->getRow(array('url'=>$data['video']));
            $data['pic'] = $pic['pic'];
            unset($data['parent_id']);
            $actual_budget = $data['actual_budget'];
            $budget = Model::factory('projectProp');
            $where = array(
                'project_budget' => array('<='=>$actual_budget)
            );
            $budget = $budget->getAll($where)->as_array();
            $budget = end($budget);
            $data['before_baifenbi'] = $budget['prop_before_sum'];
            $data['after_baifenbi']  = $budget['prop_after_sum'];
            $data['middle_baifenbi'] = 1-($data['before_baifenbi']+$data['after_baifenbi']);
//            $data['pic'] = $data['pic'][0];
            $data['c_time'] = time();
            $ret = $m_project->insert($data);
            if ($ret !== false) {
                $this->redirect('projectDemo/list');
            }
        }
        $m_projectCategory = Model::factory('projectCategory');
        $cat_list = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        $m_professionCategory = Model::factory('professionCategory');
        $cat_list2 = $m_professionCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree2 = Category::get_children_tree($cat_list2);
        $video = $video->getAll()->as_array();
        $this->content = View::factory('projectDemo/add');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->cat_list2 = $cat_list2;
        $this->content->cat_tree2 = $cat_tree2;
        $this->content->video = $video;
        $this->content->info = null;
    }

    public function action_edit(){
        $id = $_GET['id'];
        $m_projectCategory = Model::factory('projectCategory');
        $m_project = Model::factory('demo1');
        $video = Model::factory('video');
        if (!empty($_POST)) {
            $data = $_POST;
            $data['project_begin'] = strtotime($_POST['project_begin']);
            $data['project_end'] = strtotime($_POST['project_end']);
            $pic = $video->getRow(array('url'=>$data['video']));
            $data['pic'] = $pic['pic'];
            $ret = $m_project->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('projectDemo/list');
            }
        }
        $cat_list = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        $info = $m_project->getRowById($id);
        $cat_list = $m_projectCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree = Category::get_children_tree($cat_list);
        $m_professionCategory = Model::factory('professionCategory');
        $cat_list2 = $m_professionCategory->getAll(array('status'=>'open'))->as_array('id');
        $cat_tree2 = Category::get_children_tree($cat_list2);
        $video = $video->getAll()->as_array();
        $this->content = View::factory('projectDemo/edit');
        $this->content->cat_list = $cat_list;
        $this->content->cat_tree = $cat_tree;
        $this->content->cat_list2 = $cat_list2;
        $this->content->cat_tree2 = $cat_tree2;
        $this->content->video = $video;
        $this->content->info = $info;;
    }

    //假删除
    public function action_false_del()
    {
        $id = $_GET['id'];
        $m_project = Model::factory('demo1');
        $where = array('is_del'=>2);
        $ret =$m_project->updateById($where, $id);
        if ($ret !== false) {
            $this->redirect('projectDemo/list');
        }
    }

    public function action_recovery()
    {
        $model = Model::factory('demo1');
        $model = $model->getAll(array('is_del'=>2))->as_array(); //排除掉记录为未删除的demo
//        print_r($model);exit;
        $m_pro_cat = Model::factory('projectCategory');
        $m_prof_cat = Model::factory('professionCategory');
        foreach ($model as &$m)
        {
            $pro_cat = $m_pro_cat->getRow(array('id'=>$m['project_cid']));
            $m['project_cid_name'] = $pro_cat['name'];
            $prof_cat = $m_prof_cat->getRow(array('id'=>$m['profession_cid']));
            $m['profession_cid_name'] = $prof_cat['name'];
//            print_r($m);exit;
        }
//        print_r($model);exit;
        $this->content = View::factory('projectDemo/recovery');
        $this->content->model = $model;
    }

    //真删除
    public function action_really_del()
    {
        $id = $_GET['id'];
        $m_project = Model::factory('demo1');
        $ret =$m_project->deleteById($id);
        if ($ret !== false) {
            $this->redirect('projectDemo/recovery');
        }
    }

    //全部真删除
    public function action_really_allDel()
    {
        $m_project = Model::factory('demo1');
        $ret =$m_project->delete(array('is_del'=>2));
        if ($ret !== false) {
            $this->redirect('projectDemo/recovery');
        }
    }

    //恢复
    public function action_back()
    {
        $id = $_GET['id'];
        $m_project = Model::factory('demo1');
        $where = array('is_del'=>1);
        $ret =$m_project->updateById($where, $id);
        if ($ret !== false) {
            $this->redirect('projectDemo/list');
        }
    }

//    //全部恢复
//    public function action_allBack()
//    {
//        $m_project = Model::factory('demo1');
//        $where = array('is_del'=>1);
//        $ret =$m_project->update($where,'is_del');
//        if ($ret !== false) {
//            $this->redirect('projectDemo/list');
//        }
//    }

    public function action_close2() {
        $id = $_GET['id'];
        $data = array(
            'is_recommend' => 0,
        );
        $m_project = Model::factory('demo1');
        $ret = $m_project->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open2() {
        $id = $_GET['id'];
        $data = array(
            'is_recommend' => 1,
        );
        $m_project = Model::factory('demo1');
        $ret = $m_project->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }


}